package app.fugema.shop.main.order.adapter;

import android.view.View;

import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.utils.DensityUtil;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

import app.fugema.shop.R;
import app.fugema.shop.databinding.ItemOrderDetailGoodsViewBinding;
import app.fugema.shop.main.goods.view.GoodsDetailActivity;
import app.fugema.shop.main.order.model.OrderItem;
import app.fugema.shop.main.order.model.RefundOrderItem;
import app.fugema.shop.main.order.view.OrderAfterSaleDetailActivity;
import app.fugema.shop.main.order.vm.OrderListVM;

import androidx.lifecycle.LifecycleOwner;

public class OrderDetailGoodsItemMultiType extends BaseItemMultiType<OrderItem, ItemOrderDetailGoodsViewBinding>
{

    private OrderListVM orderListVM;

    public OrderDetailGoodsItemMultiType(LifecycleOwner lifecycleOwner, OrderListVM orderListVM)
    {
        super(lifecycleOwner);
        this.orderListVM = orderListVM;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.item_order_detail_goods_view;
    }

    @Override
    public void onBindViewHolder(ItemOrderDetailGoodsViewBinding binding, int position, BaseViewHolder viewHolder, OrderItem bean)
    {
        super.onBindViewHolder(binding, position, viewHolder, bean);
        if (viewHolder.getAdapter() != null)
        {
            int firstPos = viewHolder.getAdapter().getFirstPosition(BaseItemMultiType.TYPE_CHILD, 0);
            if (firstPos == position)
            {
                binding.clItemView.getDelegate().setCornerRadius_TR(DensityUtil.getDimens(R.dimen.dp_10));
                binding.clItemView.getDelegate().setCornerRadius_TL(DensityUtil.getDimens(R.dimen.dp_10));
            }
            else
            {
                binding.clItemView.getDelegate().setCornerRadius_TR(0);
                binding.clItemView.getDelegate().setCornerRadius_TL(0);
            }
            int lastPos = viewHolder.getAdapter().getLastPosition();
            if( lastPos != -1 &&  lastPos == position  )
            {
                binding.clItemView.getDelegate().setCornerRadius_BR(DensityUtil.getDimens(R.dimen.dp_10));
                binding.clItemView.getDelegate().setCornerRadius_BL(DensityUtil.getDimens(R.dimen.dp_10));
            }
            else
            {
                binding.clItemView.getDelegate().setCornerRadius_BR(0);
                binding.clItemView.getDelegate().setCornerRadius_BL(0);
            }
        }

        StringBuffer specSb = new StringBuffer();
        if(UIUtils.isNotEmpty( bean.getSize() ))
        {
            specSb.append( bean.getSizeAlias() ).append(":").append(bean.getSize()).append(" ");
        }
        if(UIUtils.isNotEmpty( bean.getColor() ))
        {
            specSb.append( bean.getColorAlias() ).append(":").append(bean.getColor()).append(" ");
        }
        if(UIUtils.isNotEmpty( bean.getVersion() ))
        {
            specSb.append( bean.getVersionAlias() ).append(":").append(bean.getVersion());
        }
        binding.tvSpecification.setText( specSb.toString() );

        //订单的当前状态  待收货 待评价 显示 申请售后 即退货退款

        // isIsCanRefund = true 并且 EnabledRefundAmount >0 RefundStats 为 null 时 显示 可以 申请售后按钮

        if (bean.isIsCanRefund()  )
        {
            //待收货 待评价  时
            if( bean.getEnabledRefundAmount() >0  && bean.getRefundStats() == 0 )
            {
                binding.rtvRefund.setVisibility(View.VISIBLE);
                binding.rtvRefund.setText( "申请售后" );
            }
            else
            {
                binding.rtvRefund.setVisibility(View.INVISIBLE);
            }
        }
        else
        {
            if( bean.getRefundStats() != 0 && bean.getOrderRefundId() != 0 )
            {
                //已经申请 售后了
                binding.rtvRefund.setVisibility(View.VISIBLE);
                //1=待商家审核,2=待买家寄货,3=待商家收货,4=商家拒绝,5=商家通过审核,6=待平台确认,7=退款成功
                if ( bean.getRefundStats() == 7  )
                {
                    binding.rtvRefund.setText("退款成功");
                }
                else
                {
                    binding.rtvRefund.setText("退款中");
                }
            }
            else
            {
                binding.rtvRefund.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, OrderItem bean)
    {
        super.onItemClick(view, adapter, onclickType, position, bean);
        switch (view.getId())
        {
            case R.id.givGoodsImage:
                GoodsDetailActivity.start(view.getContext(), bean.getProductId());
                break;

            case R.id.rtvRefund:
                //退款 退货
                if( bean.getOrderRefundId() != 0 )
                {
                    List<RefundOrderItem> refundList = new ArrayList<>();
                    RefundOrderItem mItem = new RefundOrderItem();
                    mItem.setThumbnailsUrl( bean.getProductName() );
                    mItem.setProductName( bean.getProductName() );
                    mItem.setColorAlias( bean.getColorAlias() );
                    mItem.setColor( bean.getColor() );
                    mItem.setSizeAlias( bean.getSizeAlias() );
                    mItem.setSize( bean.getSize() );
                    mItem.setVersionAlias( bean.getVersionAlias() );
                    mItem.setVersion( bean.getVersion() );
                    refundList.add( mItem );
                    //售后详情
                    OrderAfterSaleDetailActivity.startByRefundId(view.getContext(), bean.getOrderRefundId(), JacksonUtils.transBean2Json( refundList ));
                }
                else
                {
                    LogUtil.e("refundOrder","ItemId = "+bean.getItemId());
                    //activity里面   跳转选择 退款退货
                    LiveEventBus.get("refundOrder", Integer.class).post( bean.getItemId() );
                }
               break;
        }
    }
}
